package com.nys.cqhttp.message.factory.impl;

import java.util.Arrays;
import java.util.List;

import com.nys.cqhttp.model.Massage;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.nys.cqhttp.message.factory.IMessageHandler;

import com.nys.cqhttp.param.Context;
import com.nys.cqhttp.util.SpringContextUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * JiQiRenMessageHandler
 *
 * @author nysheng
 * @version 1.0.0
 * @description 机器人菲菲
 * @date 2021/12/28 15:44
 */
@Slf4j
public class JiQiRenMessageHandler implements IMessageHandler {

    @Override
    public List<String> listHandler(Massage messagePojo, Context context) {
        return Arrays.asList(handler(messagePojo, context));
    }

    @Override
    public String handler(Massage messagePojo, Context context) {
        String url = String.format(messagePojo.getValue(), context.getMessageParam().getMessage());
        RestTemplate restTemplate = SpringContextUtil.getBean("restTemplate");
        try {
            JSONObject map = JSONObject.parseObject(restTemplate.getForEntity(url, String.class).getBody());
            String result = String.valueOf(map.get("content"));
            return String.valueOf(map.get("content")).replace("{br}", "\r\n").replace("菲菲", "劳资");
        }catch (Exception e){
            return "";
        }
    }
}
